package com.jp.knowledge.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.JsonObject;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.activity.JpMainActivity;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.L;
import com.jp.knowledge.webUtil.JavaScriptInterface;
import com.jp.knowledge.webUtil.X5WebView;
import com.tencent.smtt.sdk.WebView;

/**
 * A simple {@link Fragment} subclass.
 */
public class WebFragment extends BaseFragment {


    public WebFragment() {
        // Required empty public constructor
    }



    //雷达
    public static final String follow="follow";//雷达
    public static final String headlines="headlines";//头条
    public static final String experience="experience";//体验
    public static final String discover="discover";//发现
    public static final String mine="mine";//发现我的


    private View mView;

    private X5WebView contView;
    private String url = "http://192.168.33.41:3001/";// "http://192.168.32.65:8080/test/test.html";
    public String[] web_module_name = new String[]{"headlines", "follow", "experience", "discover", "mine"};// 头条、关注、轻体验、发现、我的  headlines   follow  compete  discover mine  竞爆compete

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
//			String  str = msg.obj.toString();
            L.e("WebMainActivity========"+msg.obj.toString());
            //{"type":"toNative","param":"Follow"}
            JsonObject obj = JsonUtil.getInstance().strToJson(msg.obj.toString());
            String type = obj.get("type")==null?"":obj.get("type").getAsString();
            if(type.equals("toNative")) {
                startActivity(new Intent(mContext, JpMainActivity.class));
            }else if(type.equals("show_web")){
                contView.setVisibility(View.VISIBLE);
            }
        }
    };




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView =inflater.inflate(R.layout.fragment_web, container, false);
        initView();
        return mView;
    }

    /**
     * 初始view
     */
    private void initView() {
//        application.setWebMainActivity(this);

//        QbSdk.allowThirdPartyAppDownload(true);
        contView = (X5WebView) mView.findViewById(R.id.content);
        X5WebView.setSmallWebViewEnabled(true);// 开启webview窗口转移功能
        contView.addJavascriptInterface(new JavaScriptInterface(mHandler), "Android");
        contView.setUrlHandler(new X5WebView.UrlHandler() {
            @Override
            public void handUrl(String ul) {
                L.e("WebMainActivity");
            }

            @Override
            public void handError(WebView webView, int i, String s, String s1) {

            }
        });
        contView.loadUrl(url);
        contView.loadUrl("javascript:windowNativeBridge.setInWebView(2)");

    }


    public    void setCurrentPath(String path){
        contView.loadUrl("javascript:windowNativeBridge.setInWebView(2)");
        L.e("setCurrentPath",path);
        if(contView!=null) {
            contView.setVisibility(View.GONE);
            JsonObject obj = new JsonObject();
            obj.addProperty("type","show_web");

            contView.loadUrl("javascript:windowNativeBridge.setCurrentPath('" + path + "'),Android.jsCallback('"+obj.toString()+"')");//设置当前页面路径
        }
    }



//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK && contView.canGoBack()) {
//            contView.loadUrl("javascript:windowNativeBridge.goBack()");
//            return true;
//        }else{
//            startActivity(new Intent(context,JpMainActivity.class));
//            return true;
//        }
//
//    }

}
